#include "test.h"
#ifdef TEST_VBYTE


#include <stdio.h>
#include <assert.h>
#include "../util/vbyte.h"
#include "../util/types.h"
#include "../util/common.h"

using namespace hse;
using namespace hse::util;

void test_summary() {
	UINT l, l2;
	ULONG j;
	UBYTE b[50];
	UINT d[50];

	for (ULONG i = 0xab; i < 0x00FFFFFFFFFFFFFFL; i = (i << 8) | 0xab) {
		printf("test:%llx\n", i);
		l = VByte::EncodeAUnit(b, i);
		l2 = VByte::DecodeAUnit(&j, b);
		assert(l == l2);
		assert(i == j);
	}

	UINT src[] = {9, 128, 2000, 0, 1, 0xCFFFFFFF};
	UINT count = sizeof(src) / sizeof(UINT);
	l = VByte::EncodeAll(b, src, count);
	l2 = VByte::DecodeAll(d, b, l);
	printf("l2len:%d", l2);
	assert(l2 == count);
	for (UINT i = 0; i < count; ++i) {
		printf("src:%d(%x) dest:%d(%x)\n", src[i], src[i], d[i], d[i]);
		assert(d[i] == src[i]);
	}
}

void test_encode() {
	UINT i = 20000, j;
	UBYTE b[10];
	j = VByte::EncodeAUnit(b, i);
	assert(j == 3);
	assert(b[0] == 0x01);
	assert(b[1] == 0x1C);
	assert(b[2] == 0xA0);

	i = 0;
	j = VByte::EncodeAUnit(b, i);
	assert(j == 1);
	assert(b[0] == 0x80);

	i = 128;
	j = VByte::EncodeAUnit(b, i);
	printf("test encode 128:0x%x%x\n", b[0], b[1]);
	assert(j == 2);
	assert(b[0] == 0x01);
	assert(b[1] == 0x80);
}

void test_decode() {
	UINT r = 0;
	
	UBYTE src1[] = {0x89};
	VByte::DecodeAUnit(&r, src1);
	printf("%d(0x%x)==9?\n", r, r);
	assert(r == 9);
	

	UBYTE src2[] = {0x01, 0x80};
	VByte::DecodeAUnit(&r, src2);
	printf("%d(0x%x)==128?\n", r, r);
	assert(r == 128);
	
	UBYTE src3[] = {0x01, 0x80, 0x89, 0x01, 0x1C, 0xA0};
	UINT rs[10];
	r = VByte::DecodeAll(rs, src3, sizeof(src3));
	printf_array(rs, 3);
	assert(r == 3);
	assert(rs[0] == 128);
	assert(rs[1] == 9);
	assert(rs[2] == 20000);
}

void test_vbyte_main() {
	printf("\n*******test_decode*******\n");
	test_decode();
	printf("\n*******test_encode*******\n");
	test_encode();
	printf("\n*******test_summary*******\n");
	test_summary();
}




#endif